
// BBSimple.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "BBSimple.h"
#include "BBSimpleDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CBBSimpleApp

BEGIN_MESSAGE_MAP(CBBSimpleApp, CWinAppEx)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CBBSimpleApp construction

CBBSimpleApp::CBBSimpleApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CBBSimpleApp object

CBBSimpleApp theApp;


// CBBSimpleApp initialization

BOOL CBBSimpleApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	WSADATA wsd;
	InitW2Sock(&wsd);

	CWinAppEx::InitInstance();

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	//////////////////////////////////////////////////////////////////////////
	//GetModulePath
	TCHAR tzModulePath[MAX_PATH]={0};
	GetModuleFileName(GetModuleHandle(NULL) , tzModulePath , MAX_PATH);

	m_strModulePath.Format(_T("%s"), tzModulePath);
	int nPos = m_strModulePath.ReverseFind('\\');
	m_strModulePath.Delete(nPos, m_strModulePath.GetLength()-nPos);

	GetLogoPath();

	CBBSimpleDlg dlg;
	m_pMainWnd = &dlg;

	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

BOOL CBBSimpleApp::InitW2Sock(WSADATA * lpwsaData)
{
	WSADATA wsaData;

	if ( lpwsaData == NULL )
		lpwsaData = &wsaData;

	WORD wVersionRequested = MAKEWORD(2, 2) ;

	int nResult = WSAStartup( wVersionRequested, lpwsaData ) ;

	if (nResult != 0)
	{		
		return  FALSE;
	}

	if (LOBYTE(lpwsaData->wVersion) != 2 || HIBYTE(lpwsaData->wVersion) != 2)
	{
		WSACleanup();
		return FALSE;
	}

	return TRUE;
}

void CBBSimpleApp::InitOptionPath()
{
	TCHAR tzPath[MAX_PATH];
	wsprintf(tzPath, _T("%s\\option"), m_strModulePath);
	CreateDirectory(tzPath, NULL);

	m_strOptionPath.Format(_T("%s\\%s"), tzPath, _T("Option.ini"));

	//////////////////////////////////////////////////////////////////////////
	//Get Option file path if no option file, create default

	CString strOptionPath;
	CFileFind cFind;
	BOOL bFind;

	strOptionPath.Format(_T("%s\\Option*.ini"), tzPath);

	bFind = cFind.FindFile(strOptionPath);

	while (bFind)
	{
		bFind = cFind.FindNextFile();

		m_sarrOptionPath.Add(cFind.GetFilePath());
	}
	cFind.Close();
}

CString CBBSimpleApp::GetOptionPath(int nIndex)
{
	if (nIndex<m_sarrOptionPath.GetSize())
	{
		return m_sarrOptionPath.GetAt(nIndex);
	}
	return _T("End Of Option File");
}

void CBBSimpleApp::InitOption()
{
	m_nDemo = GetPrivateProfileInt(_T("Option"), _T("Demo"), 0, m_strOptionPath);

	dwAudioFormat = GetPrivateProfileInt(_T("Audio"), _T("Format"), 1, m_strOptionPath);
	dwAudioFrequency = GetPrivateProfileInt(_T("Audio"), _T("Frequency"), 8081, m_strOptionPath);
	dwAudioBitPerSample = GetPrivateProfileInt(_T("Audio"), _T("BitPerSample"), 16, m_strOptionPath);
}

CString CBBSimpleApp::GetLogoPath()
{
	TCHAR tzPath[MAX_PATH];
	TCHAR tzValue[MAX_PATH];
	CString strImageInfoPath;

	wsprintf(tzPath, _T("%s\\option"), m_strModulePath);
	CreateDirectory(tzPath, NULL);

	wsprintf(tzPath, _T("%s\\option\\ImagePath.ini"), m_strModulePath);

	int nSelect;
	CString strImagePath;
	nSelect = GetPrivateProfileInt(_T("Setting"), _T("Select"), 0, tzPath);

	strImagePath.Format(_T("Image Path%d"), nSelect);
	ZeroMemory(tzValue, sizeof(tzValue));
	GetPrivateProfileString(strImagePath, _T("Root Path"), NULL, tzValue, MAX_PATH, tzPath);
	m_strImageRoot.Format(_T("%s\\%s"), m_strModulePath, tzValue);
	ZeroMemory(tzValue, sizeof(tzValue));
	GetPrivateProfileString(strImagePath, _T("Info File Name"), NULL, tzValue, MAX_PATH, tzPath);
	strImageInfoPath.Format(_T("%s\\%s"), m_strImageRoot, tzValue);

	ZeroMemory(tzValue, sizeof(tzValue));

	CString strPath;
	if (GetPrivateProfileString(_T("Logo"), _T("Path"), NULL, tzValue, MAX_PATH, strImageInfoPath))
	{
		strPath.Format(_T("%s\\%s"), m_strImageRoot, tzValue);
	}
	else
	{
		strPath = _T("");
	}
	return strPath;
}